<?php

/*
 * Created on May 15, 2009
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Comment extends MY_Controller {

    function __construct() {
        parent::__construct();
        $this->load->model('news_model', 'news', true);
		$this->load->model('comment_model', 'comment', true);
        $this->checkLogin();
        $this->load->helper('form');
        $this->load->helper('text');
        $this->template->set_template('admin');
        $this->buildMenu();
        
    }

    function index() {
        //echo $this->config->item('language');
        //$this->main = '<div> erer erer er </div>';
        //$this->execute();
        //$this->load->view('main', '');
        $this->view();
    }

    function create() {
        
    }


    function edit($id) {
        $news = $this->comment->getById($id);
        $data = array();
        if (!$news) {
            redirect('admin/comment/view');
        }

        $data = $news;
        
        $data['error_message'] = '';
        $data['legend_text'] = lang('edit_news');
        
        
        $data['base_url'] = base_url();
        $data['site_url'] = site_url();
               
        $this->template->parse_view('content', 'admin/comment/create_comment', $data);
        $this->template->render();
        
    }

    function news_comment($news_id){
    	$this->view(0, -1, $news_id);
    }
    
    function view($page = 0, $active = -1, $news_id = 0) {

        $this->load->library('pagination');
	
        //$category = $this->uri->segment(4, -1);
        //$page = (int) $this->uri->segment(5);
        $config['base_url'] = site_url('admin/comment/view/' . $page . '/' . $active . '/' . $news_id . '/');

        $config['total_rows'] = $this->news->countAll($active, $news_id);
        $config['per_page'] = NUMBER_RECORD_PER_PAGE;
        $config['uri_segment'] = 3;
        $config['first_link'] = 'First';

        $this->pagination->initialize($config);
        
        $items = '';
        $news = $this->comment->getAll($page, NUMBER_RECORD_PER_PAGE, $active, $news_id);
        $data['rows'] = $news;
        $this->template->parse_view('content', 'admin/comment/comment_list', $data);
        $this->template->render();
    }

  
    function save() {
    	
        $txtID = (int) trim($this->input->post('txtID'));
       
        $data = array();
        if ($txtID > 0){
            $news = $this->comment->getById($txtID);

            if (empty($news)) {
                redirect('admin/news/view');
                return;
            }

            $data = $news;
          
        }
        else {
            $data['id'] = 0;
        }
	
        $this->load->helper(array('form', 'url'));

        $this->load->library('form_validation');

        $this->form_validation->set_rules('txtContent', 'lang:vi_description', 'required|min_length[2]');
       
        $data['content'] = trim($this->input->post('txtContent'));
        
        $data['active'] = (int) $this->input->post('active');
        
        $data['date_update'] = date('Y-m-d H:i:s', time());
        $error_message = ''; // $this->loadErrorMessage('Thong diep loi.');
        

        if ($this->form_validation->run() == FALSE) {

            $error_message = validation_errors() . '<br>' . $error_message;
            //$code = uniqid();
           
            $data['error_message'] = '<div style="color:red">' . $error_message . '</div>';
            
            $data['legend_text'] = $txtID > 0 ? lang('edit_news') : lang('create_news');
            
           
        $data['base_url'] = base_url();
        
           
            $this->template->parse_view('content', 'admin/comment/create_comment', $data);
            $this->template->render();

            
            return;
        } else {
            
            $insert_id = 0;
            if ($txtID > 0) {
                $result = $this->comment->update($data);
            } else {
				
              // $data['date_add'] = date('Y-m-d H:i:s', time());
              //  $txtID = $insert_id = $result = $this->comment->create($data);
                
            }
             
           redirect('admin/comment/view');
        }
    }
    
    

    function code_check($code) {
        $result = $this->product->getByCode($code);
        if ($result != null) {
            $this->form_validation->set_message('code_check', '%s is not available.');
            return FALSE;
        } else {
            return TRUE;
        }
    }

    function delete() {
        $id = intval($this->uri->segment(4));
        $ids = array();
        if ($id > 0) {
            $ids[] = $id;
            //$this->product->delete($id);
        } else {
            if (($this->input->post('submitDelcategory'))) {
                $ids = $this->input->post('categoryBox');
            }
        }

        if (count($ids)) {
            $result = $this->comment->delete($ids);         
        }

        redirect('admin/comment');
    }

}

?>
